<template>
	<view class="template-list">
		<uni-load-more class="loading-class" iconType="auto" v-if="templateLoading" status="loading" color="#999999" />
		<view v-else>
			<view class="template-item" v-for="(item, index) in templateList" :key="index" @click="handleSelected(item)"
				v-if="templateList.length > 0" :class="{'active': item.isSelected == true}">
				<view class="template-item-top" :style="{'background-image': 'url(' + backgroundList[index % 6] + ')'}">
					<view class="template-item-name">
						<text class="template-item-name-text">{{item.name}}</text>
					</view>
				</view>
				<view class="template-item-bottom">
					<text class="template-item-desc">{{item.tips}}</text>
				</view>
				<van-icon class="selected-icon" v-show="item.isSelected" name="success" />
			</view>

			<view class="no-template-tips" v-else>
				<van-icon class="tips-icon" name="warn-o" />
				<p class="tips-text">模板库暂无模板</p>
				<p class="tips-text">待管理员维护后可在此选择模板创建项目</p>
			</view>
		</view>
		
		<view class="height"></view>
	</view>
</template>

<script>
	import apiRqs from "@/api/api-request.js";
	export default {
		data() {
			return {
				templateList: [],
				backgroundList: ['https://biapi.vaubang.cn/UploadFiles/Images/bg-1.png',
					'https://biapi.vaubang.cn/UploadFiles/Images/bg-2.png',
					'https://biapi.vaubang.cn/UploadFiles/Images/bg-3.png',
					'https://biapi.vaubang.cn/UploadFiles/Images/bg-4.png',
					'https://biapi.vaubang.cn/UploadFiles/Images/bg-5.png',
					'https://biapi.vaubang.cn/UploadFiles/Images/bg-6.png'
				],
				templateLoading: false,
			}
		},
		onShow() {
			this.handleGetTemplateList();
		},
		methods: {
			handleSelected(item) {
				uni.setStorageSync("templateBackData", item);
				uni.navigateBack();
			},
			// 获取沃邦模板列表
			handleGetTemplateList() {
				const templateBackData = uni.getStorageSync("templateBackData");

				var that = this;
				let rqs = {
					"pageSize": 9999,
					"pageIndex": 1,
					"orderBy": ""
				};
				that.templateLoading = true;
				apiRqs._getVaubangWorkTemplateList(rqs).then((res) => {
					that.templateLoading = false;
					console.log('模板列表', res);
					if (res.code == 200) {
						that.templateList = res.data.data;
						if (templateBackData) {
							for (let i = 0; i < res.data.data.length; i++) {
								if (res.data.data[i].id == templateBackData.id) {
									res.data.data[i].isSelected = true;
								} else {
									res.data.data[i].isSelected = false;
								}
							}
						}
					} else {
						getApp().showToast(res.msg);
					}
				}).catch((err) => {
					console.log('err', err);
				})
			},
		}
	}
</script>

<style lang="less">
	page {
		background-color: #f5f5f5;
	}

	.template-list {
		.height{
			height: 40px;
		}
		.template-item {
			margin: 10px 15px;
			border-radius: 3px;
			position: relative;

			.selected-icon {
				position: absolute;
				bottom: 0;
				right: 0;
				font-size: 18px;
				color: #fff;
				background-color: #008aff;
				border-radius: 2px;
			}

			&.active {
				border: 1px solid #008aff;
			}

			.template-item-top {
				width: 100%;
				height: 90px;
				// background: radial-gradient(circle at 60% 90%,
				// 		rgba(46, 103, 161, 1),
				// 		transparent 60%),
				// 	radial-gradient(circle at 20px 20px,
				// 		rgba(46, 103, 161, 0.8),
				// 		transparent 25%),
				// 	#182336;
				// ;
				// border-radius: 3px;

				border-top-right-radius: 3px;
				border-top-left-radius: 3px;
				background-repeat: no-repeat;
				background-size: cover;
				position: relative;

				.template-item-name {
					background: rgba(0, 0, 0, 0.3);
					width: 100%;
					height: 90px;
					position: absolute;
					top: 0;
					left: 0;

					.template-item-name-text {
						font-size: 18px;
						color: #fff;
						padding: 0px 15px;
						display: inline-block;
						height: 40px;
						line-height: 40px;
						font-weight: bold;
						box-sizing: border-box;
						position: absolute;
						left: 0;
						top: 50%;
						margin-top: -20px;
					}
				}
			}

			.template-item-bottom {
				background: #fff;
				padding: 10px 15px;
				border-bottom-left-radius: 3px;
				border-bottom-right-radius: 3px;

				.template-item-desc {
					font-size: 12px;
					color: #333;
					font-weight: 500;
				}
			}
		}

		.no-template-tips {
			margin-top: 50px;
			display: flex;
			flex-direction: column;
			align-items: center;

			.tips-icon {
				font-size: 50px;
				color: #999999;
			}

			.tips-text {
				font-size: 14px;
				color: #999999;
				margin-top: 10px;
			}
		}
	}
</style>
